const app = getApp()
Page({
  data: {
    options:'',
    show: false,
    kind_show: false,
    level: '',
    levelId: '',
    kind:'',
    kinds:'',
    kindId:'',
    date_show: false,
    url:'',
    message:''
  },
  onChange(e){
    this.setData({
      message:e.detail
    })
  },
  onLoad(){
    this.setData({url:app.globalData.URL})
    const that = this
    wx.request({
      url: this.data.url+'/consult-level/selectAll',
      method:'GET',
      data:{
        pageNum:1,
        pageSize:10000,
      },
      success(resp){
        that.setData({options:resp.data.data.list.map(
          item=>({
            text:item.name,
            value:item.id
          }))
        })
      },
    })
    wx.request({
      url: this.data.url+'/specialty/list',
      method:'GET',
      data:{
        pageNum:1,
        pageSize:10000,
      },
      success(resp){
        that.setData({kinds:resp.data.data.list.map(
          item=>({
            text:item.name,
            value:item.id
          }))})
      },
    })
  },
  gore() {
    const that = this; // 保存当前页面的 this 指向
    wx.navigateTo({
      url: '/pages/index/consultation_reservation/reservation/reservation',
      success: function (res) {
        res.eventChannel.emit('onlineDetail', {
          levelId: that.data.levelId,     // 使用 that 替代 this
          typeId: that.data.kindId,       // 确保访问正确的 data
          consultText: that.data.message
        });
      }
    });
  },
  goindex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  date_showPopup() {
    this.setData({ date_show: true });
  },

  date_onClose() {
    this.setData({ date_show: false });
  },
  onClickLevel() {
    this.setData({
      show: true,
    });
  },
  onCloseLevel() {
    this.setData({
      show: false,
    });
  },
  onClickKind() {
    this.setData({ kind_show: true });
  },

  kind_onClose() {
    this.setData({ kind_show: false });
  },
  onFinish(e) {
    const { selectedOptions, value } = e.detail;
    const level = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      level,
      levelId: value,
    })
  },
  KindFinish(e){
    const { selectedOptions, value } = e.detail;
    const kind = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    this.setData({
      kind,
      kindId: value,
    })    
  }
});
